package com.study.dataStructure.generic;

/**
 * @PACKAGE_NAME: com.study.dataStructure.generic
 * @CLASS_NAME: GenericMethodDemo
 * @USER: kagura
 * @Description TODO 定义泛型方法打印一个对象数组
 * @Date 2021/1/31 4:13 下午
 * @MONTH_NAME_FULL: 一月
 * @DAY: 31
 * @DAY_NAME_FULL: 星期日
 * @PROJECT_NAME: Study
 **/
public class GenericMethodDemo {

    // 声明泛型方法需要将泛型类型设置在关键字static之后
    public static <T> void print(T[] list) {
        for (int i = 0; i < list.length; i++) {
            System.out.print(list[i] + " ");
        }
        System.out.println();
    }

    // main
    public static void main(String[] args) {
        Integer[] integers = {1, 3, 5, 7, 9};
        String[] strings = {"重庆", "北京", "天津", "上海"};

        // 调用泛型方法，需要将实际类型放在尖括号内作为方法的前缀
        GenericMethodDemo.<Integer> print(integers);
        GenericMethodDemo.<String> print(strings);

        // 或者直接调用简单调用，由编译器自动发现实际类型
        print(integers);
        print(strings);
    }
}
